#ifndef _READ_AFOF_
#define _READ_AFOF_
#include "data_readers.h"
/////////////////// Typedefs for Galaxy sorting //////
class CGalaxy;

typedef std::pair <std::vector<CGalaxy*>::iterator, float> Map_Int_Flt_Pair;

typedef std::pair <int, int> Map_Int_Int_Pair;

////////////////////CGalaxy class ////////
class CGalaxy{
public:
	CGalaxy(){
		memset(x,0,sizeof(x));		
		memset(npart,0,sizeof(npart));
		Ntotal=0;R=0;ID=0;m_Rxx=0;		
		};

	~CGalaxy(){};

	/////////////////////////////
/*	inline bool operator == (const CGalaxy &b) const
		{
		return (b.grpID==grpID);
		}
		*/
	/////////////////////////////	
	vector<Map_Int_Flt_Pair> child_list;
	vector<int> id;
	vector<int> idref;
	vector<float> dist_rxx;
	float x[3];
	float R;
	float m_Rxx;
	int ID;
//	int grpID;
	int npart[3];
	//int Rxyz[3];
	int Ntotal;


	};

typedef std::map< unsigned int, CGalaxy *> TGalaxyMap;
//////////////////: AFOF CLASS /////////////////
class CFOFCatalog : public CReaders
	{
	public:
		CFOFCatalog():RadFrac(1.0){IDs=NULL;};
		CFOFCatalog(string file);
		~CFOFCatalog(){
			TGalaxyMap::iterator ithalos=halos.begin();
			while(ithalos!=halos.end())
				{
					//cout<<"~CFOFCatalog: deleting"<<endl;
				delete  (*ithalos).second;
					//ithalos=halos.erase(ithalos);
					++ithalos;
				};
			halos.clear();

			};
		bool ReadCatalog(string file);
		bool ReadCatalogIDs(string file);
		bool ReadCatalogIDsBin(string file);
		bool InitIDs( int *ID)
			{
			  if(m_verbose)
			    cout<<"Init IDS ...";
			bool ret=false;
			string file=m_afofcat, filebin;
			file.resize(m_afofcat.size()-4);
			IDs=ID;
			filebin=file;
			filebin+=string(".bin");
			if(m_verbose)
			  cout<<".RB.";
			ret=ReadCatalogIDsBin(filebin);
			if(!ret)
				{
				  if(m_verbose)
				    cout<<".RA.";
				  ret=ReadCatalogIDs(file);
				}
			if(m_verbose)
			  cout<<"ok"<<endl;
			return ret;
			}
		
		inline unsigned int GetNhalo(void){return halos.size();}
		CGalaxy* GetGalByID(unsigned int );  
		TGalaxyMap halos;
		void SetWeightForIDs(float *pRho){m_pWeight=pRho;};
		//vector<unsigned int> grpIDs;
		int *IDs;
		float *m_pWeight;
		//CGadget* m_gadobj;
		//float Redshift;
		float RadFrac;
		//vector<int> indexVector;// used for sorting 
		//vector<int>::iterator it;// used for accessing to sorted elements
		string m_afofcat;

	};
#endif

